package com.lnking.lnkingplace.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lnking.lnkingplace.mapper.PlaceCheckConfigMapper;
import com.lnking.lnkingplace.model.entity.PlaceCheckConfig;
import com.lnking.lnkingplace.model.entity.PlaceInfo;
import com.lnking.lnkingplace.model.request.place.PlaceCheckConfigRequest;
import com.lnking.lnkingplace.service.IPlaceCheckConfigService;
import com.lnking.lnkingplace.service.IPlaceInfoService;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sn
 * @since 2022-09-20
 */
@Service
public class PlaceCheckConfigServiceImpl extends CustomServiceImpl<PlaceCheckConfigMapper, PlaceCheckConfig> implements IPlaceCheckConfigService {


    @Autowired
    IPlaceInfoService placeInfoService;
    @Override
    public void placeCheckConfig(PlaceCheckConfigRequest params) {
        QueryWrapper<PlaceInfo> swapper = new QueryWrapper<>();
        swapper.lambda().in(PlaceInfo::getPlaceType,params.getPlaceType());
        List<PlaceInfo> placeInfos = placeInfoService.getBaseMapper().selectList(swapper);
        for (PlaceInfo placeInfo : placeInfos) {
            PlaceCheckConfig placeCheckConfig = getBaseMapper().selectByPlaceId(placeInfo.getId());
            if(placeCheckConfig == null){
                placeCheckConfig =new PlaceCheckConfig();
                placeCheckConfig.setPlaceId(placeInfo.getId());
                placeCheckConfig.setConfigDay(params.getConfigDay());
                getBaseMapper().insert(placeCheckConfig);
            }else{
                placeCheckConfig.setConfigDay(params.getConfigDay());
                UpdateWrapper<PlaceCheckConfig> wapper =new UpdateWrapper<>();
                wapper.lambda().eq(PlaceCheckConfig::getPlaceId,placeCheckConfig.getPlaceId());
                getBaseMapper().update(placeCheckConfig,wapper);
            }

        }
    }
}
